﻿// 文件名：函数和结构体
// 创建时间：2021/6/21/星期一 17:05:11

#include <iostream>
using namespace std;
//定义旅行时间结构体
struct travel_time
{
	int hours;
	int minutes;
};

//使用结构体处理数据
const int Mins_per_hr = 60;
travel_time sum(travel_time t1, travel_time t2);
void show_time(travel_time t);
void travel()
{
	travel_time day1 = { 5,45 };
	travel_time day2 = { 4,15 };
	travel_time trip = sum(day1, day2);
	cout << "Two days total = ";
	show_time(trip);
	travel_time day3 = { 4,12 };
	cout << "Three days total = ";
	travel_time trip2 = sum(trip, day3);
	show_time(trip2);

}
travel_time sum(travel_time t1, travel_time t2)//对实参的拷贝
{
	travel_time t_new;
	t_new.hours = t1.hours + t2.hours;
	int mins = t1.minutes + t2.minutes;
	if (mins >= Mins_per_hr)
	{
		t_new.minutes = mins % Mins_per_hr; //取余
		t_new.hours++;
		cout << t_new.minutes << endl;
	}
	else
	{
		t_new.minutes = mins;
	}
	return t_new; //直接返回结构体
}
void show_time(travel_time t)
{
	cout << t.hours << " hours,";
	cout << t.minutes << " minutes.\n";
}


//实现直角坐标系和极坐标的转换
#include <cmath>
struct polar
{
	double distance;
	double angle;
};
struct rect
{
	double x;
	double y;
};

polar rect2polar(rect xypos);
void show_polar(polar dapos);
void show_polar(const polar* dapos);
void atrctfun()
{
	rect rplace;
	polar pplace;
	cout << "Enter the x and y values: ";
	while (cin >> rplace.x >> rplace.y) //slick use of cin灵活运用cin
	{
		pplace = rect2polar(rplace);
		//使用值传递方式访问
		show_polar(pplace);
		//使用指针传递方式访问
		show_polar(&pplace);
		cout << "Next two numbers(q to quit): ";
	}
	cout << "Done\n";
}
polar rect2polar(rect xypos)
{
	polar answer;
	answer.distance = sqrt(xypos.x*xypos.x + xypos.y*xypos.y);
	answer.angle = atan2(xypos.y, xypos.x);
	return answer;
}
void show_polar(polar dapos)
{
	const double Rad2deg = 57.29577951;
	cout << "r = " << dapos.distance << ", theta = " << dapos.angle*Rad2deg << " degrees\n";
}
//使用指针访问结构体
void show_polar(const polar* dapos)//const修饰指针，禁止程序修改数据
{
	const double Rad2deg = 57.29577951;
	cout << "r = " << dapos->distance << ", theta = " << dapos->angle*Rad2deg << " degrees\n";
}

int main()
{
	//使用结构体处理数据
	//travel();

	//程序实现直角坐标系和极坐标系的转换
	atrctfun();

	system("pause");
	return 0;
}